package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.AskActivity;
import com.example.administrator.szb.activity.SearchActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.common.SetStatusBar;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.fragments.fragmenttool.FragmentZX;
import com.example.administrator.szb.fragments.fragmenttool.anli.FragmentAnli;
import com.example.administrator.szb.fragments.fragmenttool.falv.FragmentFalv;
import com.example.administrator.szb.fragments.fragmenttool.luyan.FragmentLuYan;

/**
 * Created by stone on 2018/1/20.
 */

public class FragmentTools  extends MVPBaseFragment {
    private XTabLayout tabLayout;
    private ImageView new_ask,search;
    private FragmentManager fm;
    private FragmentZX fragmentZX;
    private FragmentLuYan fragmentLuYan;
    private FragmentAnli fragmentAnli;
    private FragmentFalv fragmentFalv;
    private boolean isLoad = false;
    private int selects = 0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ask,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        SetStatusBar.setBar(view,getContext());
        tabLayout = (XTabLayout)view.findViewById(R.id.tabLayout);
        new_ask = view.findViewById(R.id.new_ask);
        new_ask.setVisibility(View.GONE);
        search = view.findViewById(R.id.search);
        fm = getChildFragmentManager();
    }
    public FragmentTransaction getFragmentTransaction(){

        FragmentTransaction transaction = fm.beginTransaction();

        return transaction;
    }
    @Override
    protected void initEvent() {
        isLoad = true;
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        new_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(getActivity(), AskActivity.class));
            }
        });


        tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                showFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void initData() {
        initTabLayout("投行资讯");
        initTabLayout("路演调研");
        initTabLayout("案例学习");
        initTabLayout("法规查询");
        tabLayout.getTabAt(selects).select();

    }
    private void initTabLayout(String str){
        tabLayout.addTab(tabLayout.newTab().setText(str));
    }

    private void showFragment(int i){
        FragmentTransaction transaction = getFragmentTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if (fragmentZX==null){
                    fragmentZX = new FragmentZX();
                    transaction.add(R.id.askcontent,fragmentZX);
                }else {
                    transaction.show(fragmentZX);
                }

                break;
            case 1:
                if (fragmentLuYan==null){
                    fragmentLuYan = new FragmentLuYan();
                    transaction.add(R.id.askcontent,fragmentLuYan);
                }else {
                    transaction.show(fragmentLuYan);
                }

                break;
            case 2:
                if (fragmentAnli==null){
                    fragmentAnli = new FragmentAnli();
                    transaction.add(R.id.askcontent,fragmentAnli);
                }else {
                    transaction.show(fragmentAnli);
                }

                break;
            case 3:
                if (fragmentFalv==null){
                    fragmentFalv = new FragmentFalv();
                    transaction.add(R.id.askcontent,fragmentFalv);
                }else {
                    transaction.show(fragmentFalv);
                }

                break;
        }

        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction){
        if (fragmentZX!=null){
            transaction.hide(fragmentZX);
        }
        if (fragmentLuYan!=null){
            transaction.hide(fragmentLuYan);
        }
        if (fragmentAnli!=null){
            transaction.hide(fragmentAnli);
        }
        if (fragmentFalv!=null){
            transaction.hide(fragmentFalv);
        }
    }

    public void setSelectTab(int selectTabs){
        this.selects  = selectTabs;
        if (isLoad){
            tabLayout.getTabAt(selects).select();
        }
    }
}